//
//  XMPrivacySetView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/21.
//

#import "XMPrivacySetView.h"
#import "XMPrivacySetTypeOneCell.h"
#import "XMPrivacySetTypeTwoCell.h"

@interface XMPrivacySetView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;

@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *subTitleArr;

@end

@implementation XMPrivacySetView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"隐私设置";
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(14.0) frame:CGRectMake(self.width - KScaleWidth(8.0) - KScaleWidth(55.0), kNavagationBarH - KScaleWidth(17.0) - KScaleWidth(24.0), KScaleWidth(55.0), KScaleWidth(24.0)) title:@"保存" titleColor:kColorHEX(0x333333)];
    [saveBtn setRadius:saveBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
    [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:saveBtn];
    _titleArr = @[@[@"允许查看我的关注/粉丝列表", @"谁可以私信我"], @[@"主页访客记录", @"推荐管理"], @[@"只允许关注我的人评论我", @"只允许关注我的人@我", @"我的收藏", @"我喜欢的", @"找到我的方式"]];
    _subTitleArr = @[@[@"关闭后只可看到数字，不可查看详情列表", @""], @[@"开启后你可以查看自己主页的访客记录。同时你查看他人主页时也会留下记录。", @"管理个性化推荐内容的展示"], @[@"", @"", @"不公开", @"不公开", @"不公开"]];
    [self addSubview:self.tbl];
}

- (void)saveAction:(UIButton *)button {
    
}

#pragma mark - Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 2) {
        return 5;
    }
    else {
        return 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return KScaleWidth(8.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(8.0))];
    footView.backgroundColor = kColorHEX(0xF0F0F0);
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return KScaleWidth(75.0);
        }
        else {
            return KScaleWidth(45.0);
        }
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            return KScaleWidth(93.0);
        }
        else {
            return KScaleWidth(75.0);
        }
    }
    else {
        return KScaleWidth(45.0);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMPrivacySetCellName"];
    if (indexPath.section == 0 || indexPath.section == 1) {
        if (indexPath.section == 0 && indexPath.row == 1) {
            return [self tableView:tableView typeTwoCellForRowAtIndexPath:indexPath];
        }
        else {
            return [self tableView:tableView typeOneCellForRowAtIndexPath:indexPath];
        }
    }
    else {
//        if (indexPath.row < 2) {
//            return [self tableView:tableView typeOneCellForRowAtIndexPath:indexPath];
//        }
//        else {
            return [self tableView:tableView typeTwoCellForRowAtIndexPath:indexPath];
//        }
    }
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
  typeOneCellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger type = 1;
    if (indexPath.row == 0) {
        type = 0;
    }
    XMPrivacySetTypeOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMPrivacySetTypeOneCellName"];
    [cell setContentWithTitle:_titleArr[indexPath.section][indexPath.row] subTitle:_subTitleArr[indexPath.section][indexPath.row] type:type];
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
  typeTwoCellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger type = 1;
    if (indexPath.section == 2 && (indexPath.row == 0 || indexPath.row == 1)) {
        type = 0;
    }
    XMPrivacySetTypeTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMPrivacySetTypeTwoCellName"];
    [cell setContentWithTitle:_titleArr[indexPath.section][indexPath.row] subTitle:_subTitleArr[indexPath.section][indexPath.row] type:type];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.separatorColor = kColorHEX(0xF1F1F1);
        _tbl.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tbl.backgroundColor = kColorHEX(0xF0F0F0);
        [_tbl registerClass:[XMPrivacySetTypeOneCell class] forCellReuseIdentifier:@"XMPrivacySetTypeOneCellName"];
        [_tbl registerClass:[XMPrivacySetTypeTwoCell class] forCellReuseIdentifier:@"XMPrivacySetTypeTwoCellName"];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
